

$(function () {
    var employee_datagird = $("#employee_datagird");
    var employee_dialog = $("#employee_dialog");
    var image_dialog = $("#image_dialog");
    var employee_add_form = $("#employee_add_form");
    var role = $("#role");
    employee_datagird.datagrid({
        url:"/employee/list",
        fit:true,
        fitColumns:true,
        pagination:true,//分页
        border:false,
        rownumbers:true,
        striped:true,//行号 斑马线
        singleSelect:true,//单选
        sortOrder:"desc",//排序
        columns:
            [[
                {field:'imagePath',title:'用户头像',width:100,formatter:formattershowImg},
                {field:'username',title:'用户名',width:100},
                {field:'realName',title:'姓名',width:100},
                {field:'tel',title:'电话',width:100},
                {field:'email',title:'邮箱',width:100},
                {field:'dept',title:'部门',width:100,formatter:formatterDept},
                {field:'inputTime',title:'录入时间',width:100},
                {field:'state',title:'状态',width:100,formatter:formatterState},
                {field:'admin',title:'管理员',width:100,formatter:formatterAdmin}


            ]],
        toolbar:"#employee_datagrid_toolbar"
    });

    employee_dialog.dialog({
        buttons:"#employee_dialog_buttons",
        closed:true
    });


    image_dialog.dialog({
        buttons:"#image_dialog_buttons",
        closed:true
    });

    var cmdObj = {
        addEmp : function () {
            employee_add_form.form("clear");
            employee_dialog.dialog("setTitle","新增");
            employee_dialog.dialog("open");
        },
        editEmp : function () {
            //选中一行
            var selectedRow = employee_datagird.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("提示","老铁,请先选中一条数据","error");
                return;
            }

            employee_add_form.form("clear");
            //数据回显
            $.post("/role/listRoleByEmployeeId",{employeeId:selectedRow.id},function (data) {
                role.combobox("setValue",data);
            })
            if(selectedRow.dept) {
                selectedRow['dept.id'] = selectedRow.dept.id;
            }
            employee_add_form.form("load",selectedRow);
            employee_dialog.dialog("setTitle","编辑");
            employee_dialog.dialog("open");
        },
        removeEmp : function () {
            //选中一行
            var selectedRow = employee_datagird.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("提示","老铁,请先选中一条数据","error");
                return;
            }

            $.messager.confirm("提示","老铁,您确定要离职[ "+selectedRow.username+" ]员工吗?",function (yes) {
                if(yes){
                    $.post("/employee/delete",{id:selectedRow.id},function (data) {
                        if(data.success){
                            $.messager.alert("提示","离职成功","info",function () {
                                cmdObj.cancel();
                                cmdObj.reloadEmp();
                            });
                        }else{
                            $.messager.alert("提示",data.msg,"error");
                        }
                    })
                }
            })
        },
        reloadEmp : function () {
            employee_datagird.datagrid("reload");
        },
        save : function () {
            var url = "/employee/save";
            var idValue = $("#employee_id").val();
            if(idValue){
                url = "/employee/edit";
            }
            employee_add_form.form("submit",{
                url:url,
                onSubmit:function(param){
                    var values = role.combobox("getValues");
                    for (var i = 0;i < values.length;i ++){
                        param['roles['+ i +'].id'] = values[i];
                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            cmdObj.cancel();
                            cmdObj.reloadEmp();
                        });
                    }else{
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            })
        },
        cancel : function () {
            employee_dialog.dialog("close");
        },
        search : function () {
            var keyword = $("#keyword").val();
            employee_datagird.datagrid("load",{keyword:keyword});
        },
        export: function () {
            window.location.href="/employee/export";
        }
    };

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

});

function showImage(tagert) {
    $("#image_dialog").dialog("open");
    // var selectedRow = $("#employee_datagird").datagrid("getSelected");
    $("#bigImage").attr("src",tagert.src);
    console.log(tagert);
}

function formattershowImg(value, row, index){
    if(row.imagePath) {
                return "<img onclick='showImage(this)' style='width:30px;height:30px;' border='1' src='" + row.imagePath + "'/>"
            }
        }







function formatterDept(value,row,index) {
    return value ? value.name : "";
}

function formatterState(value,row,index) {
    if(value == 0){
        return "正常";
    }else{
        return "<span style='color: red;'>离职</span>"
    }
}

function formatterAdmin(value,row,index) {
    if(value){
        return "是";
    }
    return "否";
}